<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_detail.css">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
</head>
<body>
    <div class="nav">
        <img src="image/logo2.jpg" alt="">
        <div class="title">我的博客系统</div>
        <div class="spacer"></div>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>
    <div class="container">
        <div class="container-left">
            <div class="card">
                <img src="image/kun.jpg" alt="">
                <h3></h3>
                <a href="https://www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>

        <div class="container-right"> 
            <h3></h3>
            <div class="date"></div>
            <div class="content" id="content"> 
            </div>
            <!-- 添加删除博客的链接 -->
            <div class="delete-blog">
                <a href="#" id="deleteBlogButton">删除博客</a>
            </div>
        </div>
    </div>

    <script src="js/app.js"></script>
    
    
    <script>
        // 显示完整博客
        function getBlog() {
            $.ajax({
                type: 'get',
                url: 'blog' + location.search,
                success: function(blog) {
                    let h3 = document.querySelector('.container-right h3');
                    h3.innerHTML = blog.title;
                    let dateDiv = document.querySelector('.container-right .date');
                    dateDiv.innerHTML = blog.postTime;
                    editormd.markdownToHTML('content', { markdown: blog.context });
                }
            })
        }
        getBlog();

        // 显示用户名
        function getAuthorInfo() {
            $.ajax({
                type: 'get',
                url: 'getAuthor' + location.search,
                success: function(author) {
                    let h3 = document.querySelector('.card h3');
                    h3.innerHTML = author.username;
                }
            });
        }
        getAuthorInfo();

        // 删除博客功能
        document.getElementById('deleteBlogButton').addEventListener('click', function (event) {
            event.preventDefault(); // 防止默认链接行为
            if (confirm('您确定要删除这篇博客吗？')) {
                $.ajax({
                    type: 'get',
                    url: 'deleteBlog' + location.search, // 也需要带上 blogId 参数
                    success: function(response) {
                        alert('博客已成功删除！');
                        window.location.href = 'blog_list.html'; // 重定向到博客列表页面
                    },
                    error: function() {
                        alert('删除博客失败，请重试！');
                    }
                });
            }
        });

        // 判断登录状态
        getLoginStatus();
    </script>
</body>
</html>
